﻿// <copyright Autor="twitter.com/gotactile">
// Distributed under Microsoft Public License (Ms-PL)
// </copyright>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.ComponentModel;

namespace Con10uum
{ 
	/// <summary>
	/// Logique d'interaction pour UserControl1.xaml
	/// </summary>
	public class Con10uumWindow : HeaderedContentControl
	{
        public static readonly RoutedEvent ClosedEvent = EventManager.RegisterRoutedEvent("Closed", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Con10uumWindow));

        [Category("Behavior")]
        public event RoutedEventHandler Closed
        {
            add
            {
                base.AddHandler(ClosedEvent, value);
            }
            remove
            {
                base.RemoveHandler(ClosedEvent, value);
            }
        }

        public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent("Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Con10uumWindow));

        [Category("Behavior")]
        public event RoutedEventHandler Tap
        {
            add
            {
                base.AddHandler(TapEvent, value);
            }
            remove
            {
                base.RemoveHandler(TapEvent, value);
            }
        }

        static Con10uumWindow()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Con10uumWindow), new FrameworkPropertyMetadata(typeof(Con10uumWindow)));
            UIElement.FocusableProperty.OverrideMetadata(typeof(Con10uumWindow), new FrameworkPropertyMetadata(false));
            Control.IsTabStopProperty.OverrideMetadata(typeof(Con10uumWindow), new FrameworkPropertyMetadata(false));
        }

        public Con10uumWindow()
		{
			this.IsManipulationEnabled = true;
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
		}

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.ManipulationStarting += new EventHandler<ManipulationStartingEventArgs>(this_ManipulationStarting);
            this.ManipulationDelta += new EventHandler<ManipulationDeltaEventArgs>(this_ManipulationDelta);
            this.ManipulationCompleted += new EventHandler<ManipulationCompletedEventArgs>(this_ManipulationCompleted);
            this.ManipulationBoundaryFeedback += new EventHandler<ManipulationBoundaryFeedbackEventArgs>(this_ManipulationBoundaryFeedback);

            //Not sure this is the good way to procede here :
            try
            {
                ContentControl obj2 = (ContentControl)this.Template.FindName("HeaderSite", this);
                ContentPresenter pres = (ContentPresenter)VisualTreeHelper.GetChild(obj2, 0);
                Button b = (Button)obj2.ContentTemplate.FindName("CloseButton", pres);
                if (b != null)
                {
                    b.Click += new RoutedEventHandler(b_Click);
                }
            }
            catch 
            {
                
                
            }

            this.TouchUp += new EventHandler<TouchEventArgs>(Con10uumWindow_TouchUp);
        }

        void Con10uumWindow_TouchUp(object sender, TouchEventArgs e)
        {
            base.RaiseEvent(new RoutedEventArgs(TapEvent, this));
        }

        void b_Click(object sender, RoutedEventArgs e)
        {
            base.RaiseEvent(new RoutedEventArgs(ClosedEvent, this));
        }

        void this_ManipulationBoundaryFeedback(object sender, ManipulationBoundaryFeedbackEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Mani boundary " + e.BoundaryFeedback.Translation.ToString()); 
        }

        void this_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
        {
            try
            {
                var touchcount = this.TouchesOver.Count();
            
                var generalTouchcount = ((FrameworkElement)this.Parent).TouchesOver.Count();

                if (touchcount == 3 && generalTouchcount == 3)
                {
                    e.ManipulationContainer = this.Template.FindName("Con10uumWindowRoot", this) as FrameworkElement;

                    // you could set the mode here too 
                    e.Mode = ManipulationModes.Scale;
                    e.Handled = true;
                }
            }
            catch
            {
            }
        }

        void this_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            try
            {
                var touchcount = this.TouchesOver.Count();

                var generalTouchcount = ((FrameworkElement)this.Parent).TouchesOver.Count();

                if (touchcount == 3 && generalTouchcount == 3)
                {
                    var deltaManipulation = e.DeltaManipulation;

                    double width = (double)this.Width;
                    if (Double.IsNaN(width))
                    {
                        width = this.RenderSize.Width;
                    }
                    double newWidth = width * (deltaManipulation.Scale.X);
                    double windowMinWidth = (double)this.Parent.GetValue(Con10uum.WindowMinWidthProperty);
                    double windowMaxWidth = (double)this.Parent.GetValue(Con10uum.WindowMaxWidthProperty);

                    this.Width = Math.Min(Math.Max(newWidth, windowMinWidth), windowMaxWidth);
                    //Con10uum c = this.Parent as Con10uum;
                    //if (c != null)
                    //{
                    //    Con10uumPanel p = c.Template.FindName("Con10uumPanel", c) as Con10uumPanel;
                    //    if (p != null)
                    //    {
                    //        p.MakeVisible(this, new Rect() { Width = this.Width });
                    //    }
                    //}
                    e.Handled = true;
                }
            }
            catch
            {
            }
        }

        void this_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
        }
	}
}